﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using HISModel;
using Newtonsoft.Json;
namespace HISDAL
{
    public class OfficeDal
    {
        //链接数据库字符串
        SqlConnection con = new SqlConnection("Data Source=169.254.82.63;Initial Catalog=HospitalSystem;User ID=sa;password=asd_123");

        //添加科室信息
        public int AddOffice(T_Office m)
        {
            con.Open();
<<<<<<< .mine

            string sql = "";
||||||| .r32
<<<<<<< .mine
            string sql = string.Format("insert into T_Office values('{0}','{1}')", m.Office_Name, m.Office_Mess);
            SqlCommand cmd = new SqlCommand(sql, con);
||||||| .r24
            string sql = string.Format("insert into T_Office values('{0}','{1}')",m.Office_Name,m.Office_Mess);
            SqlCommand cmd = new SqlCommand(sql,con);
=======
            string sql = "";
=======

            string sql = string.Format("insert into T_Office values('{0}','{1}')", m.Office_Name, m.Office_Mess);
            SqlCommand cmd = new SqlCommand(sql, con);

>>>>>>> .r33
            //判断传递过来的类中是否存在ID值 不存在说明是添加   存在则是修改
            if (m.Office_ID == 0)
            {
                sql += string.Format("insert into T_Office values('{0}','{1}')", m.Office_Name, m.Office_Mess);
            }
            else
            {
                sql += string.Format("update T_Office set Office_Name = '{0}', Office_Mess = '{1}' where Office_Id='{2}'",m.Office_Name,m.Office_Mess,m.Office_ID);
            }
<<<<<<< .mine
           
            SqlCommand cmd = new SqlCommand(sql,con);

||||||| .r32
           
            SqlCommand cmd = new SqlCommand(sql,con);
>>>>>>> .r30
=======

>>>>>>> .r33
            int res = cmd.ExecuteNonQuery();
            con.Close();
            return res;
        }
        //获取科室信息
        public List<T_Office> SelectOff(string name)
        {

            string sql = "select * from T_Office where 1=1";

            if (!string.IsNullOrWhiteSpace(name))
            {
                sql += "  and Office_Name like '%" + name + "%'";
            }

            SqlDataAdapter adp = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            adp.Fill(dt);
            var s = JsonConvert.SerializeObject(dt);
            var list = JsonConvert.DeserializeObject<List<T_Office>>(s);
            return list;
        }
        /// <summary>
        /// 科室分页查询
        /// </summary>
        /// <param name="id">单挑查询的医院ID值</param>
        /// <returns></returns>
        public T_Officeuserinfos<T_Office> T_Officefenye(int pageindex, int pagesize, string Office_ID, string T_Office)
        {
            string sql = "select * from " + T_Office + "";
            SqlCommand cmd = new SqlCommand("Pro_wannengfenye", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@pageindex", pageindex);
            cmd.Parameters.AddWithValue("@pagesize", pagesize);
            cmd.Parameters.AddWithValue("@order", Office_ID);
            cmd.Parameters.AddWithValue("@tablename", T_Office);
            cmd.Parameters.AddWithValue("@totalCount", SqlDbType.Int);
            cmd.Parameters["@totalCount"].Direction = ParameterDirection.Output;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable de = new DataTable();
            da.Fill(de);
            var totalcount = (int)cmd.Parameters["@totalCount"].Value;
            var data = JsonConvert.SerializeObject(de);
            var result = JsonConvert.DeserializeObject<List<T_Office>>(data);
            T_Officeuserinfos<T_Office> list = new T_Officeuserinfos<T_Office>();
            list.Itemlist = result;
            list.pageindex = pageindex;
            list.pagesize = pagesize;
            if (totalcount % pagesize == 0)
            {
                list.totalcount = totalcount / pagesize;
            }
            else
            {
                list.totalcount = totalcount / pagesize + 1;
            }
            return list;
        }
        //删除科室信息
        public int DelOffice(int id)
        {
            con.Open();
            string sql = string.Format("DELETE FROM dbo.T_Office WHERE Office_ID='{0}'", id);
            SqlCommand cmd = new SqlCommand(sql, con);
            int res = cmd.ExecuteNonQuery();
            con.Close();
            return res;
        }
        
    }
}
